package top.jolyoulu.modules.redismodule.lock;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @Author LuZhouJin
 * @Date 2023/5/31
 * Key规则管理器，里面封装的很多Key生成策略
 */
@Component
public class KeyRuleManager {

    private final static HashMap<Class<? extends KeyRule>, KeyRule> map = new HashMap<>();

    static {
        map.put(DefKetRule.class,new DefKetRule());
    }

    /**
     * 添加一个Key策略
     * @param keyRule KeyRule的实现类
     */
    public void add(KeyRule keyRule){
        map.put(keyRule.getClass(),keyRule);
    }

    /**
     * 获取一个key策略
     * @param keyRule KeyRule实现类的class
     * @return 返回一个KeyRule
     */
    public KeyRule get(Class<? extends KeyRule> keyRule){
        return map.get(keyRule);
    }

    /**
     * 删除一个key策略
     * @param keyRule KeyRule实现类的class
     */
    public void del(Class<? extends KeyRule> keyRule){
        map.remove(keyRule);
    }
}
